<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.prime.com.tr/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html">

    <body>
        <ui:composition template="../templates/default3.xhtml">
            <ui:define name="content">
                <h:form binding="#{paymentManagerBean.loadAddPayment}">
                    <p:panel header="Customer Details" id="customerDetailsPanel">
                        <table width="100%" border="0">
                            <tr>
                                <td width="200px">
                                    Company:
                                </td>
                                <td>
                                    <h:outputText id="company" value="#{paymentManagerBean.selectedCustomer.company}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Description:
                                </td>
                                <td>
                                    <h:outputText id="description" value="#{paymentManagerBean.selectedCustomer.description}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Annual Revenue:
                                </td>
                                <td>
                                    <h:outputText id="revenue" value="#{paymentManagerBean.selectedCustomer.revenue}">
                                        <f:convertNumber type="currency"/>
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Number of Employees:
                                </td>
                                <td>
                                    <h:outputText id="numEmployee" value="#{paymentManagerBean.selectedCustomer.numEmployee}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Contact Person
                                </td>
                                <td>
                                    <h:outputText id="contactPerson" value="#{paymentManagerBean.selectedCustomer.firstName}, 
                                                  #{paymentManagerBean.selectedCustomer.lastName}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Designation:
                                </td>
                                <td>
                                    <h:outputText id="title" value="#{paymentManagerBean.selectedCustomer.title}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Contact Number:
                                </td>
                                <td>
                                    <h:outputText id="phone" value="#{paymentManagerBean.selectedCustomer.phone}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Credit Rank:
                                </td>
                                <td>
                                    <h:outputText id="rank" value="#{paymentManagerBean.selectedCustomer.credit.creditRank}" />
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Credit Limit:
                                </td>
                                <td>
                                    <h:outputText id="limit" value="#{paymentManagerBean.selectedCustomer.credit.creditLimit}">
                                        <f:convertNumber type="currency"/>
                                    </h:outputText>
                                </td>
                            </tr>
                            <tr>
                                <td width="200px">
                                    Balance:
                                </td>
                                <td>
                                    <h:outputText id="balance" value="#{paymentManagerBean.selectedCustomer.credit.balance}">
                                        <f:convertNumber type="currency"/>
                                    </h:outputText>
                                </td>
                            </tr>
                        </table>
                    </p:panel>
                    <p:panel header="Sales Orders" id="salesOrdersPanel">
                        <p:dataTable id="salesOrders" var="salesOrder" value="#{paymentManagerBean.salesOrders}" 
                                     emptyMessage="No sales orders found with given criteria."
                                     selection="#{paymentManagerBean.selectedSalesOrder}"
                                     selectionMode="single"
                                     rowSelectListener="#{paymentManagerBean.onRowSelect}"
                                     onRowSelectUpdate="orderDetailsPanel" 
                                     rowUnselectListener="#{paymentManagerBean.onRowUnselect}"
                                     onRowUnselectUpdate="orderDetailsPanel">
                            <p:column filterBy="#{salesOrder.job.purchaseOrder.id}" headerText="PO ID" 
                                      filterMatchMode="contains" sortBy="#{salesOrder.job.purchaseOrder.id}">  
                                <h:outputText value="#{salesOrder.job.purchaseOrder.id}"/> 
                            </p:column> 
                            <p:column headerText="Total Amount" sortBy="#{salesOrder.job.purchaseOrder.totalAmount}">  
                                <h:outputText value="#{salesOrder.job.purchaseOrder.totalAmount}">  
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Credit Rate (%)" sortBy="#{salesOrder.job.purchaseOrder.creditRate}">  
                                <h:outputText value="#{salesOrder.job.purchaseOrder.creditRate}">
                                </h:outputText>
                            </p:column> 
                        </p:dataTable>
                    </p:panel>
                    <p:panel header="Order Details" id="orderDetailsPanel">
                        <p:dataTable id="purchaseItems" var="purchaseItem" value="#{paymentManagerBean.purchaseItems}" 
                                     emptyMessage="No purchase items found with given criteria."
                                     selection="#{paymentManagerBean.selectedPurchaseItem}"
                                     selectionMode="single"
                                     rowSelectListener="#{paymentManagerBean.onRowSelect}"
                                     rowUnselectListener="#{paymentManagerBean.onRowUnselect}">
                            <p:column headerText="Product" sortBy="#{purchaseItem.product.name}">  
                                <h:outputText value="#{purchaseItem.product.name}">
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Quantity" sortBy="#{purchaseItem.quantity}">  
                                <h:outputText value="#{purchaseItem.quantity}">
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Total Amount" sortBy="#{purchaseItem.subTotal}">  
                                <h:outputText value="#{purchaseItem.subTotal}">  
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Outstanding Amount" sortBy="#{purchaseItem.outstandingAmount}">  
                                <h:outputText value="#{purchaseItem.outstandingAmount}">
                                    <f:convertNumber type="currency"/>
                                </h:outputText>
                            </p:column> 
                            <p:column headerText="Status" sortBy="#{purchaseItem.status}">  
                                <h:outputText value="#{purchaseItem.status}">
                                </h:outputText>
                            </p:column> 
                        </p:dataTable>
                    </p:panel>
                    <p:panel header="Make Payment" id="paymentPanel">
                        <p:messages />
                        <table width="100%" border="0">
                            <tr>
                                <td width="150px">Invoice ID:</td>
                                <td>
                                    <p:inputText id="invoiceId" value="#{paymentManagerBean.invoiceId}" 
                                                 required="true" requiredMessage="Invoice ID cannot be empty."
                                                 title="ID of invoice to pay for.">
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td width="150px">Amount:</td>
                                <td>
                                    <p:inputText id="payment" value="#{paymentManagerBean.amountPaid}" 
                                                 required="true" requiredMessage="Amount paid cannot be empty."
                                                 title="Amount to pay.">
                                        <f:convertNumber minFractionDigits="2" /> 
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                </td>
                            </tr>
                            <tr>
                                <td width="150px">Balance Used:</td>
                                <td>
                                    <p:inputText id="balance2" value="#{paymentManagerBean.balanceUsed}" 
                                                 required="true" requiredMessage="Balance to use cannot be empty."
                                                 title="Balance to use.">
                                        <f:convertNumber minFractionDigits="2" /> 
                                    </p:inputText>
                                    <font class="fieldNote">*</font>
                                                            
                                </td>
                            </tr>
                            <tr><td></td>
                                <td>
                                    <p:commandButton id="btnRecordPayment" value="Record" 
                                         title="Record the payment."
                                         actionListener="#{paymentManagerBean.addPayment}"
                                         ajax="true" update="paymentPanel"/>
                                </td>
                            </tr>
                        </table>

                    </p:panel>
                </h:form>
                <p:ajaxStatus onstart="statusDialog.show();" onsuccess="statusDialog.hide();"/>  
                <p:dialog modal="true" widgetVar="statusDialog" header="Please wait..."   
                          draggable="false" closable="false" resizable="false">  
                    <p:graphicImage value="../resources/images/ajaxloadingbar.gif" />  
                </p:dialog>
            </ui:define>
        </ui:composition>
    </body>
</html>
